package jhavelogger.parser;

import java.util.HashSet;

import jhavelogger.models.AcceptedConnection;
import jhavelogger.models.UsernameLookup;
import jhavelogger.models.UnparsedLog;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;

public class UsernameLookupFactoryTests extends LogFactoryTestingBase {

    private HashSet<LogMessageType> allowedLogMessageTypes;

    @Before
    public void setUp() throws Exception {
	super.setUp();
	this.factory = new UsernameLookupFactory();
	this.allowedLogMessageTypes = new HashSet<LogMessageType>();
	this.allowedLogMessageTypes.add(LogMessageType.USERNAMELOOKUPNOUSERNAME);
	this.allowedLogMessageTypes.add(LogMessageType.USERNAMELOOKUPWITHUSERNAME);
    }

    @Test
    public void setsAllNeededPropertiesWithUsername() {
	String line = "1/21/09 10:19 AM - FINE: Up front with user  naps@uwosh.edu looking for -- exe/dfsrichard/dfsrichard";
	UsernameLookup log = (UsernameLookup) this.factory.create(new UnparsedLog("127.0.0.1", line));
	
	Assert.assertNotNull("A log object must be created", log);
	Assert.assertEquals("Username must match expected value", "naps@uwosh.edu", log.getUsername());
    }
    
    @Test
    public void setsAllNeededPropertiesNoUsername() {
	String line = "1/21/09 4:07 AM - FINE: Up front with user  no_username looking for -- exe/memorymanager/memorymanager";
	UsernameLookup log = (UsernameLookup) this.factory.create(new UnparsedLog("127.0.0.1", line));
	
	Assert.assertNotNull("A log object must be created", log);
	Assert.assertEquals("Username must match expected value", "no_username", log.getUsername());
    }
    
    @Test
    public void setsAllNeededPropertiesUnspecifiedUsername() {
	String line = "3/18/08 10:45 AM - FINE: up front looking for -- exe/bestfirstsearch/bestfirstsearch";
	UsernameLookup log = (UsernameLookup) this.factory.create(new UnparsedLog("127.0.0.1", line));
	
	Assert.assertNotNull("A log object must be created", log);
	Assert.assertEquals("Username must match expected value", "no_username", log.getUsername());
    }
    
    @Test
    public void parsesUsernameLookupLogs() {
	Assert.assertTrue(this.factoryCreatesObject(LogMessageType.USERNAMELOOKUPNOUSERNAME));
	Assert.assertTrue(this.factoryCreatesObject(LogMessageType.USERNAMELOOKUPWITHUSERNAME));
    }

    @Test
    public void doesNotParseOtherLogs() {
	Assert.assertNull(this.factoryDoesNotCreateObjectsOfTypesOtherThan(this.allowedLogMessageTypes));
    }
}
